<template>
  <view class="pay">
    <!-- 收货地址 -->
    <view>
      <u-button
        @click="onChooseAddress"
        v-if="!address"
      >获取收货地址</u-button>
      <view v-else>
        <view>{{detailAddress}}</view>
        <view>{{address.userName}} {{address.telNumber}}</view>
      </view>
    </view>
    <!-- 已选商品 -->
    <view class="pay-goods">
      <view class="pay-goods-title">已选商品</view>
      <view class="pay-goods-list">
        <!-- 要循环的商品 -->
        <view
          class="pay-goods-item"
          v-for="item in goods"
          :key="item.goods_id"
        >
          <view class="goods-img">
            <image
              mode="widthFix"
              :src="item.goods_small_logo"
            ></image>
          </view>
          <view class="goods-info">
            <view class="goods-name">{{item.goods_name}}</view>
            <view class="goods-price-count">
              <view class="goods-price">￥{{item.goods_price}}</view>
              <view class="goods-count">X{{item.count}}</view>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 支付  -->
    <view class="pay-tool">
      <view class="price">
        合计： <text>￥{{price}}</text>
      </view>
      <view
        class="pay-btn"
        @tap="onWxPay"
      >去支付({{count}})</view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      address: null,
    };
  },
  computed: {
    detailAddress() {
      if (!this.address) return "";
      const { provinceName, cityName, countyName, detailInfo } = this.address;
      return provinceName + cityName + countyName + detailInfo;
    },
    // 返回已经勾选的了商品
    goods() {
      return this.$store.getters.checkedGoods;
    },
    // 总的价格
    price() {
      return this.$store.getters.countAll;
    },
    // 总的购买数量
    count() {
      return this.$store.getters.checkedCount;
    },
    token() {
      return this.$store.state.token;
    },
  },
  methods: {
    // 获取 微信收货地址
    async onChooseAddress() {
      const [err, res] = await uni.chooseAddress({});
      this.address = res;
    },
    // 执行 去支付
    async onWxPay() {
      // 1 先判断一下有没有获取收货地址
      if (!this.address) {
        uni.showToast({ title: "您没有选择收货地址", icon: "none" });
        return;
      }
      // 2 判断有没有token
      if (!this.token) {
        uni.navigateTo({ url: "/pages/auth/auth" });
        return;
      }

      // 3 完成创建订单
      // 3.1 构造 创建订单的接口的参数
      const orderParams = {
        order_price: this.price, // 订单的总价格
        consignee_addr: this.detailAddress, // 收货地址
        // 商品数组
        goods: this.goods.map((g) => ({
          goods_id: g.goods_id,
          goods_number: g.count,
          goods_price: g.goods_price,
        })),
      };
      // 3.2 发送请求 完成订单的创建
      const res4 = await this.$u.api.getOrderNumber(orderParams);
      const { order_number } = res4.message;

      // 4 获取 支付参数
      const res5 = await this.$u.api.getPay({ order_number });
      const { pay } = res5.message;

      // 5 调起微信支付
      const [err6, res6] = await uni.requestPayment(pay);
      console.log(err6);
      console.log(res6);
    },
  },
};
</script>

<style lang="scss">
.pay {
  padding-bottom: 80rpx;
}
.pay-goods {
  padding: 10rpx;
  background-color: #fff;
  .pay-goods-title {
    padding: 15prx 0;
  }

  .pay-goods-list {
    .pay-goods-item {
      display: flex;
      border-bottom: 1rpx solid #eee;
      .goods-img {
        flex: 1;
        image {
          width: 100%;
        }
      }

      .goods-info {
        flex: 2;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        .goods-name {
        }

        .goods-price-count {
          display: flex;
          justify-content: space-between;
          .goods-price {
            color: #eb4450;
          }

          .goods-count {
          }
        }
      }
    }
  }
}
.pay-tool {
  background-color: #fff;
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  border-top: 1rpx solid #ccc;
  display: flex;
  justify-content: space-between;
  height: 80rpx;
  align-items: center;
  padding: 0 10rpx;
  .price {
    text {
      color: #eb4450;
    }
  }

  .pay-btn {
    background-color: #eb4450;
    color: #ffffff;
    width: 160rpx;
    height: 50rpx;
    border-radius: 25rpx;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>